package GUI;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.geom.Vector2f;

import Game.GameHelper;
import Tools.SoundManager;

/** GUI main menu class
 * @author Aikidos
 */
public class GuiMainMenu {

	/** Menu items */
	private List<String> _items = new ArrayList<String>();
	
	/** Menu normal font */
	private UnicodeFont _font;
	
	/** Selected font */
	private UnicodeFont _fontSelected;

	/** Selected item index */
	private int _selectedItem = 0;
	
	/** Vertical draw offset */
	private float _verticalOffset = 5.f;
	
	/** Timer of control update */
	private int _timerControlUpdate = 0;
	
	public GuiMainMenu() throws SlickException {
		// Initialize fonts
		_font = new UnicodeFont("res/fonts/mainfont.ttf", 11, false,
						false);
		_font.addAsciiGlyphs();
		_font.getEffects().add(new ColorEffect(java.awt.Color.LIGHT_GRAY));
		_font.loadGlyphs();
		
		_fontSelected = new UnicodeFont("res/fonts/mainfont.ttf", 15, false,
				false);
		_fontSelected.addAsciiGlyphs();
		_fontSelected.getEffects().add(new ColorEffect(java.awt.Color.BLACK));
		_fontSelected.loadGlyphs();
	}
	
	/** Returns selected index
	 * @return Selected index
	 */
	public int getSelected() {
		return _selectedItem;
	}
	
	public void addItem(String item) {
		_items.add(item);
	}
	
	public void update(int delta) {
		// Control
		Input input = GameHelper.getGame().getInput();
		if (input.isKeyPressed(Input.KEY_DOWN)) {
			if (_selectedItem + 1 < _items.size()) _selectedItem++;
			else _selectedItem = 0;
			
			// Play sound
			SoundManager.play("sfx_menu");
		}
		if (input.isKeyPressed(Input.KEY_UP)) {
			if (_selectedItem - 1 >= 0) _selectedItem--;
			else _selectedItem = _items.size() - 1;
			
			// Play sound
			SoundManager.play("sfx_menu");
		}
	}
	
	public void draw(Vector2f drawPos, Graphics g) {
		
		Vector2f currPos = new Vector2f(drawPos);
		
		for (int i = 0; i < _items.size(); i++) {
			if (i == _selectedItem) {
				_fontSelected.drawString(currPos.x, currPos.y, _items.get(i));
				currPos.y += _fontSelected.getHeight(_items.get(i)) + _verticalOffset;
			} else {
				_font.drawString(currPos.x, currPos.y, _items.get(i));
				currPos.y += _font.getHeight(_items.get(i)) + _verticalOffset;
			}
		}
	}
	
}
